// lib/page/SplashScreen.dart
import 'package:flutter/material.dart';

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  int _countdown = 3;
  bool _isCancelled = false; // 添加取消标志

  @override
  void initState() {
    super.initState();
    _startCountdown();
  }

  void _startCountdown() {
    Future.delayed(const Duration(seconds: 1), () {
      if (mounted && !_isCancelled) {
        setState(() {
          _countdown--;
        });
        if (_countdown > 0) {
          _startCountdown();
        } else {
          _navigateToHome();
        }
      }
    });
  }

  // 添加跳转方法
  void _navigateToHome() {
    if (!_isCancelled && mounted) {
      Navigator.pushReplacementNamed(context, '/');
    }
  }

  // 添加跳过方法
  void _skipSplash() {
    _navigateToHome();
    _isCancelled = true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: [
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset('assets/images/loginLogo.jpg', width: 100, height: 100),
                const SizedBox(height: 20),
                const Text('应用名称', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
                const SizedBox(height: 10),

              ],
            ),
          ),
          Positioned(
            top: 20,
            right: 20,
            child:  Text('$_countdown s', style: const TextStyle(color: Colors.grey)),
          ),
          // 添加跳过按钮在右上角
          Positioned(
            top: 50,
            right: 20,
            child: TextButton(
              onPressed: _skipSplash,
              child: const Text(
                '跳过',
                style: TextStyle(
                  color: Colors.grey,
                  fontSize: 16,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
